package hu.bme.mit.rifl.business.base;

import hu.bme.mit.rifl.datamodel.Order;

import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;

/**
 * A Queue on the output end of the workflow node. Extends the RiflOrderQueue
 * abstract class.
 */
public class RiflOrderOutputQueue extends RiflOrderQueue {

	private MessageProducer producer;

	public RiflOrderOutputQueue(String queueName) {
		super(queueName);
		try {
			producer = getMessageProducer();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Puts a single order into the respective queue.
	 * 
	 * @param order
	 *            workitem of type hu.bme.mit.rifl.datamodel.Order
	 */
	public void put(Order order) {
		try {
			ObjectMessage message = this.session.createObjectMessage();
			message.setObject(order);
			producer.send(message);
		} catch (JMSException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Creates a MessageProducer.
	 * 
	 * @return The MessageProducer instance which is able to send a workitem.
	 * @throws Exception
	 */
	public MessageProducer getMessageProducer() throws Exception {
		MessageProducer messageProducer = this.session
				.createProducer(this.queue);
		return messageProducer;
	}
}
